---
import ArticleItem from "@components/common/article/ArticleItem.astro"

---
<th:block th:each="article,iterStat : ${data.rows}">
    <ArticleItem classAppend="${''}"/>
</th:block>

<script>
  function formatRelativeTime(publishTimestamp) {
    const currentTime = new Date();
    const publishDate = new Date(publishTimestamp);
    const timeDifference = currentTime - publishDate;

    // 转换为分钟
    const minutesAgo = Math.floor(timeDifference / (1000 * 60));
    // 转换为小时
    const hoursAgo = Math.floor(minutesAgo / 60);
    // 转换为天数
    const daysAgo = Math.floor(hoursAgo / 24);
    // 转换为月数（简单估计，实际月份天数不一）
    const monthsAgo = Math.floor(daysAgo / 30);
    // 转换为年数（同样为简单估计）
    const yearsAgo = Math.floor(monthsAgo / 12);

    if (minutesAgo < 1) {
      return "刚刚";
    } else if (minutesAgo < 60) {
      return `${minutesAgo}分钟前`;
    } else if (hoursAgo < 24) {
      return `${hoursAgo}小时前`;
    } else if (daysAgo < 30) {
      return `${daysAgo}天前`;
    } else if (monthsAgo < 12) {
      return `${monthsAgo}个月前`;
    } else {
      return `${yearsAgo}年前`;
    }
  }
  
  // 转换为K
  function convertToK(readCount) {
    if (readCount >= 1000) {
      return (readCount / 1000).toFixed(1) + "K";
    } else {
      return readCount.toString();
    }
  }

    const createTimeDom = document.querySelectorAll(".show-create-time");
    const showKdom = document.querySelectorAll(".show-k");
    createTimeDom.forEach((item) => {
      if (item.dataset.time) {
        item.innerHTML = formatRelativeTime(new Date(item.dataset.time).getTime()); // 假设这是文章的发布时间戳
      } else {
      }
    });
    showKdom.forEach((item) => {
      if (item.dataset.count) {
        item.innerHTML = convertToK(item.dataset.count);
      } else {
      }
    });
</script>
